package com.west_traveler.recyclerview_adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.west_traveler.entity.entity_anchor.Item_list_Anchor;
import com.west_traveler.entity.entity_live_page.entity_live_second_page.RadioInfo;
import com.west_traveler.ximalayafm.R;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;


/**
 * created by Wang_JunJie at 2016/12/7
 */
public class NormalLiveSecondRecyclerAdapter extends RecyclerView.Adapter<NormalLiveSecondRecyclerAdapter.NormalLiveViewHolder> {

    private Context context;
    private List<RadioInfo> datas;
    private OnNormalLiveClickListener listener;
    private RecyclerView recycler;

    public NormalLiveSecondRecyclerAdapter(Context context) {
        this.context = context;
        datas=new LinkedList<>();
    }

    public void setListener(OnNormalLiveClickListener listener){
        this.listener=listener;
    }

    @Override
    public NormalLiveViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.frg_live_peritem, parent, false);
        NormalLiveViewHolder viewHolder = new NormalLiveViewHolder(view);
        return viewHolder;
    }
    public void addDatas(Collection<? extends RadioInfo> collection){
        int size = datas.size();
        datas.addAll(collection);
        notifyItemRangeInserted(size, collection.size());
    }

    @Override
    public void onBindViewHolder(NormalLiveViewHolder holder, int position) {
        RadioInfo info = datas.get(position);
        Picasso.with(context).load(info.getRadioCoverLarge()).into(holder.ivPic);
        holder.tvRadioName.setText(info.getRname());
        holder.tvProgramName.setText(info.getProgramName());
        holder.tvPlayCount.setText(info.getRadioPlayCount()+"");
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recycler=recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recycler=null;
    }



    public class NormalLiveViewHolder extends RecyclerView.ViewHolder{


        private  TextView tvPlayCount;
        private  ImageView ivPic;
        private  TextView tvRadioName;
        private  TextView tvProgramName;

        public NormalLiveViewHolder(View itemView) {
            super(itemView);

            ivPic = ((ImageView) itemView.findViewById(R.id.iv_peritem_id));
            tvRadioName = ((TextView) itemView.findViewById(R.id.tv_peritem_radioname_id));
            tvProgramName = ((TextView) itemView.findViewById(R.id.tv_peritem_programName_id));
            tvPlayCount = ((TextView) itemView.findViewById(R.id.tv_peritem_playCount_id));
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.i("调试","适配器中的onclick");
                    if (recycler != null && listener != null) {
                        int position = recycler.getChildAdapterPosition(view);
                        long id = getItemId();
                        listener.onNormalLiveClick(position, id, datas.get(position));
                    }
                }
            });
        }
    }


    //自定义一个点击回调接口
    public interface OnNormalLiveClickListener{
        void onNormalLiveClick(int position,long id,RadioInfo radioInfo);
    }
}
